from typing import List
from .model import Student
from ninja import Field, ModelSchema, Query, Router
from ninja.pagination import paginate
from utils.fu_crud import (
    ImportSchema,
    create,
    delete,
    export_data,
    import_data,
    retrieve,
    update,
)
from utils.fu_ninja import FuFilters, MyPagination

router = Router()


# 设置过滤字段
class Filters(FuFilters):
    student_school: str = Field(None, alias='student_school')
    mother_id_card_number: str = Field(None, alias='mother_id_card_number')
    mother_name: str = Field(None, alias='mother_name')
    father_id_card_number: str = Field(None, alias='father_id_card_number')
    father_name: str = Field(None, alias='father_name')
    residence_location: str = Field(None, alias='residence_location')
    household_address: str = Field(None, alias='household_address')
    current_address: str = Field(None, alias='current_address')
    ethnicity: str = Field(None, alias='ethnicity')
    gender: str = Field(None, alias='gender')
    contact_phone: int = Field(None, alias='contact_phone')
    student_number: str = Field(None, alias='student_number')
    id_card_number: str = Field(None, alias='id_card_number')
    name: str = Field(None, alias='name')
    class_teacher_phone: str = Field(None, alias='class_teacher_phone')
    class_teacher: str = Field(None, alias='class_teacher')
    class_number: int = Field(None, alias='class_number')
    gerder: str = Field(None, alias='gerder')


# 设置请求接收字段
class StudentSchemaIn(ModelSchema):
    class Config:
        model = Student
        model_exclude = ['create_datetime', 'update_datetime', 'belong_dept', 'modifier', 'creator', 'id']


# 设置响应字段
class StudentSchemaOut(ModelSchema):
    class Config:
        model = Student
        model_fields = '__all__'


# 创建Student
@router.post('/student', response=StudentSchemaOut)
def create_student(request, data: StudentSchemaIn):
    student = create(request, data, Student)
    return student


# 删除Student
@router.delete('/student/{id}')
def delete_student(request, id: int):
    delete(id, Student)
    return {'success': True}


# 更新Student
@router.put('/student/{id}', response=StudentSchemaOut)
def update_student(request, id: int, data: StudentSchemaIn):
    student = update(request, id, data, Student)
    return student


# 获取Student
@router.get('/student', response=List[StudentSchemaOut])
@paginate(MyPagination)
def list_student(request, filters: Filters = Query(...)):
    qs = retrieve(request, Student, filters)
    return qs


# 导入
@router.get('/student/all/export')
def export_student(request):
    export_fields = (
        'gerder', 'class_number', 'class_teacher', 'class_teacher_phone', 'name', 'id_card_number', 'student_number',
        'contact_phone', 'gender', 'ethnicity', 'current_address', 'household_address', 'residence_location',
        'father_name', 'father_id_card_number', 'mother_name', 'mother_id_card_number', 'student_school',)
    return export_data(request, Student, StudentSchemaOut, export_fields)


# 导出
@router.post('/student/all/import')
def import_student(request, data: ImportSchema):
    import_fields = (
        'gerder', 'class_number', 'class_teacher', 'class_teacher_phone', 'name', 'id_card_number', 'student_number',
        'contact_phone', 'gender', 'ethnicity', 'current_address', 'household_address', 'residence_location',
        'father_name', 'father_id_card_number', 'mother_name', 'mother_id_card_number', 'student_school',)
    return import_data(request, Student, StudentSchemaIn, data, import_fields)
